package com.fingard.csp.common.date;

import com.fingard.csp.common.text.StringHelper;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class DateHelper {
    /**
     * 0表示月份相等，-1表示p_calendar1小于p_calendar2，1表示p_calendar1大于p_calendar2
     */
    public static int compareMonth(Calendar p_calendar1, Calendar p_calendar2) {
        int tmpYM1 = Integer.parseInt(Format.Date6YearMonth.format(p_calendar1.getTime()));
        int tmpYM2 = Integer.parseInt(Format.Date6YearMonth.format(p_calendar2.getTime()));
        if (tmpYM1 == tmpYM2) {
            return 0;
        } else if (tmpYM1 < tmpYM2) {
            return -1;
        } else {
            return 1;
        }
    }

    /**
     * 0表示相等，-1表示p_d1小于p_d2，1表示p_d1大于p_d2
     */
    public static int compareMonth(Date p_d1, Date p_d2) {
        int tmpD1 = Integer.parseInt(Format.Date6YearMonth.format(p_d1));
        int tmpD2 = Integer.parseInt(Format.Date6YearMonth.format(p_d2));
        if (tmpD1 == tmpD2) {
            return 0;
        } else if (tmpD1 < tmpD2) {
            return -1;
        } else {
            return 1;
        }
    }

    /**
     * 0表示相等，-1表示p_d1小于p_d2，1表示p_d1大于p_d2
     */
    public static int compareDay(Date p_d1, Date p_d2) {
        int tmpD1 = Integer.parseInt(Format.Date8Format.format(p_d1));
        int tmpD2 = Integer.parseInt(Format.Date8Format.format(p_d2));
        if (tmpD1 == tmpD2) {
            return 0;
        } else if (tmpD1 < tmpD2) {
            return -1;
        } else {
            return 1;
        }
    }

    /**
     * 相差秒数，p_d1-p_d2
     */
    public static double getDifferSecs(Date p_d1, Date p_d2) {
        long a = p_d1.getTime();
        long b = p_d2.getTime();
        double c = (a - b) / 1000;
        return c;
    }

    /**
     * 相差毫秒数，p_d1-p_d2
     */
    public static long getDifferMilSecs(Date p_d1, Date p_d2) {
        long a = p_d1.getTime();
        long b = p_d2.getTime();
        return a - b;
    }

    /**
     * 获取自1970年1月1日 0点0分0秒以来的秒数，10位数字
     */
    public static String getSecsFrom1970() {
        Date tmp1970;
        try {
            tmp1970 = Format.DateTimeFormat.parse("1970-01-01 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
        double diffSecs = DateHelper.getDifferSecs(new Date(), tmp1970);
        long tmpSecs = Long.valueOf(String.format("%1$.0f", diffSecs)).longValue();
        return String.format("%10d", tmpSecs);
    }

    /**获取小时数，比如22时*/
    public static int getHour(Date p_date){
    	Calendar tmpCalendar = Calendar.getInstance();
    	tmpCalendar.setTime(p_date);
    	return tmpCalendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 相差分钟，p_d1-p_d2
     */
    public static double getDifferMins(Date p_d1, Date p_d2) {
        long a = p_d1.getTime();
        long b = p_d2.getTime();
        double aa = a;
        double bb = b;
        double c = (aa - bb) / 60000;
        return c;
    }

    /**
     * 相差小时，p_d1-p_d2
     */
    public static double getDifferHours(Date p_d1, Date p_d2) {
        long a = p_d1.getTime();
        long b = p_d2.getTime();
        double c = (a - b) / 3600000;
        return c;
    }

    /**
     * 相差天数，p_d1-p_d2
     */
    public static double getDifferDays(Date p_d1, Date p_d2) {
        long a = p_d1.getTime();
        long b = p_d2.getTime();
        double c = (a - b) / 86400000;
        return c;
    }

    public static double getDifferDays2(Date p_d1, Date p_d2) {
        long a = p_d1.getTime();
        long b = p_d2.getTime();
        double c = (a*1.00 - b) / 86400000;
        return c;
    }
    /**
     * 相差月数，p_d1-p_d2
     */
    public static int getDifferMonths(Date p_d1, Date p_d2) {
        Calendar afferent = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        afferent.setTime(p_d1);
        now.setTime(p_d2);
        int year = (now.get(Calendar.YEAR) - afferent.get(Calendar.YEAR)) * 12;
        int month = now.get(Calendar.MONTH) - afferent.get(Calendar.MONTH);
        return Math.abs(year + month);
    }

    /**
     * 获取日期格式字符串 yyyy-mm-dd
     */
    public static String getFormat12Date(String date) {
        String formatDate = "";
        if (date.indexOf("/") > 0) {
            formatDate = formatDate.replaceAll("/", "-");
        } else {
            formatDate = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
        }
        return formatDate;
    }

    /**
     * 参数date:格式为2016-01-01 12:00:01
     * 格式为2016/01/01 12:00:01
     * 格式为2016-01-01
     * 格式为2016/01/01 12:00:01
     * 返回日期格式字符串
     * type:1: yyyymmdd
     */
    public static String getFormat8Date(String date, String type) {
        String formatDate = "";
        if (date.length() > 10) {//判断如果是带时分秒格式，长度大于10
            date = date.substring(0, 10);
        }
        if (date.indexOf("/") > 0) {
            formatDate = formatDate.replaceAll("/", "-");
        } else {
            formatDate = date.substring(0, 4) + date.substring(4, 6) + date.substring(6, 8);
        }
        return formatDate;
    }

    /**
     * 获取日期格式字符串 yyyy-mm-dd hh:mm:ss
     */
    public static String getFormat8Time(String time) {
        String formatTime = "";
        if (time.length() < 6) {
            time = "0" + time;
        } else {
            formatTime = time.substring(0, 2) + ":" + time.substring(2, 4) + ":" + time.substring(4, 6);
        }
        return formatTime;
    }

    /**
     * 加减分钟
     *
     * @param startTime
     * @param interval
     * @return
     */
    public static Date addMinutes(Date startTime, int interval) {
        if (startTime == null) {
            return null;
        }
        long ms = startTime.getTime() + interval * 60 * 1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);
        return calendar.getTime();
    }

    /**
     * 加减秒
     *
     * @param startTime
     * @param interval
     * @return
     */
    public static Date addSeconds(Date startTime, long interval) {
        if (startTime == null) {
            return null;
        }
        long ms = startTime.getTime() + interval * 1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);
        return calendar.getTime();
    }

    public static Date addDays(Date startTime, int dCount) {
        if (startTime == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.DATE, dCount);
        return calendar.getTime();
    }

    public static Date addMonths(Date startTime, int mCount) {
        if (startTime == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.MONTH, mCount);
        return calendar.getTime();
    }

    public static boolean isSameDay(Date date1, Date date2) {
        String day1 = Format.Date8Format.format(date1);
        String day2 = Format.Date8Format.format(date2);
        return day1.equals(day2);
    }

    /**
     * yyyyMMdd转成yyyy-MM-dd
     */
    public static String convertD8toD10(String p_date) {
        if (p_date.length() == 8) {
            return p_date.substring(0, 4) + "-" + p_date.substring(4, 6) + "-" + p_date.substring(6, 8);
        } else {
            return p_date;
        }
    }
    /**
     * 八位日期转十位，可指定分隔符
     */
    public static String convertD8toD10(String p_date,String splitStr) {
        if (p_date.length() == 8) {
            return p_date.substring(0, 4) + splitStr + p_date.substring(4, 6) + splitStr + p_date.substring(6, 8);
        } else {
            return p_date;
        }
    }

    /**
     * yyyyMMdd yyyy-MM-dd yyyy/MM/dd yyyyMMddHHmmss 转 yyyyMMdd
     * @param p_date yyyyMMdd yyyy-MM-dd yyyy/MM/dd yyyyMMddHHmmss
     * @return  yyyyMMdd
     */
    public static String convertDateTimetoD8(String p_date) {
        if(!StringHelper.hasAnyChar(p_date)){
            return "";
        }else if (p_date.length() == 8) {
            return p_date;
        }else if(p_date.length() >= 10){
            String tmpDate = p_date.substring(0,10);
           if(tmpDate.contains("-")||tmpDate.contains("/")){
               return tmpDate.substring(0, 4) + tmpDate.substring(5, 7) + tmpDate.substring(8, 10);
           }else{
               return p_date.substring(0,8);
           }
        }else{
            return p_date;
        }
    }

    /**
     * yyyyMMddhhmmss 转 yyyy-MM-dd hh:mm:ss
     */
    public static String convertDateTime(String p_date) {
        if(!StringHelper.hasAnyChar(p_date)){
            return "";
        }else if(p_date.length()==14){
           return p_date.substring(0, 4) + "-" + p_date.substring(4, 6) + "-" + p_date.substring(6, 8) + " " + p_date.substring(8, 10) + ":" + p_date.substring(10, 12) + ":" + p_date.substring(12, 14);
        }else{
            return p_date;
        }
    }

    /**
     * yyyy-MM-dd转成yyyyMMdd
     */
    public static String convertD10toD8(String p_date) {
        if (p_date.length() >= 10) {
            return p_date.substring(0, 4) + p_date.substring(5, 7) + p_date.substring(8, 10);
        } else {
            return p_date;
        }
    }

    /**根据字符串长度转日期*/
    public static Date convertToDateExtra(String p_strDate) throws ParseException{
    	if(p_strDate.length() == 8){
			return Format.Date8Format.parse(p_strDate);
		}
		else if(p_strDate.length() == 10){
			return Format.Date10Format.parse(p_strDate);
		}
		else if(p_strDate.length() == 14){
			return Format.DateTime14Format.parse(p_strDate);
		}
		else if(p_strDate.length() == 19){
			return Format.DateTimeFormat.parse(p_strDate);
		}
    	return null;
    }
}
